//
//  MainTableViewCell.m
//  waiMai
//
//  Created by LONG on 16/4/15.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MainTableViewCell.h"

@interface MainTableViewCell()

@property(nonatomic,strong) UIImageView *iconView;//头像
@property(nonatomic,strong) UILabel *nameView;//昵称
@property(nonatomic,strong) UILabel *priceView;//起送价格
@property(nonatomic,strong) UILabel *labprice;//配送价格

@property(nonatomic,strong)UIImageView *xingImage;
@property(nonatomic,strong)UIImageView *backImage;

@property(nonatomic,strong) UILabel *evaluateView;//评分
@property(nonatomic,strong) UILabel *someView;//销售量
@property(nonatomic,strong) UILabel *juliView;//距离
@property(nonatomic,strong) UILabel *youhuiView;//优惠
@property(nonatomic,strong) UIView *bai;//qisong
@property(nonatomic,strong) UIView *xian;//qisong


@end

@implementation MainTableViewCell



//头像view
- (UIImageView *)iconView
{
    if(_iconView ==nil)
    {
        _iconView =[[UIImageView alloc]init];
        [self.contentView addSubview:_iconView];
    }
    return _iconView;
}
//昵称
- (UILabel *)nameView
{
    if(_nameView ==nil)
    {
        _nameView =[[UILabel alloc]init];
        _priceView.font = [UIFont systemFontOfSize:14];

        _nameView.numberOfLines = 0;
        [self.contentView addSubview:_nameView];
    }
    return _nameView;
}
//价格
- (UILabel *)priceView
{
    if(_priceView ==nil)
    {
        _priceView =[[UILabel alloc]init];
        _priceView.font = [UIFont systemFontOfSize:12];
        _priceView.textColor = UIColorFromRGB(0x666666);
       
        [self.contentView addSubview: _priceView];
    }
    return _priceView;
}
- (UILabel *)labprice
{
    if(_labprice ==nil)
    {
        _labprice=[[UILabel alloc]init];
        _labprice.font = [UIFont systemFontOfSize:11];
        _labprice.textColor = UIColorFromRGB(0x666666);

        [self.contentView addSubview: _labprice];
    }
    return _labprice;
}
//星星view
- (UIImageView *)xingImage
{
    if(_xingImage ==nil)
    {
        _xingImage =[[UIImageView alloc]init];
        _xingImage.image = [UIImage imageNamed:@"星星1"];
        [self.contentView addSubview:_xingImage];
    }
    
    return _xingImage;
}
//星星back
- (UIImageView *)backImage
{
    if(_backImage ==nil)
    {
        _backImage =[[UIImageView alloc]init];
        _backImage.image = [UIImage imageNamed:@"星星2"];
        [self.contentView addSubview:_backImage];
    }
    
    return _backImage;
}

//评分
- (UILabel *)evaluateView
{
    if(_evaluateView ==nil)
    {
        _evaluateView =[[UILabel alloc]init];
        _evaluateView.font = [UIFont systemFontOfSize:13];
        _evaluateView.textColor = UIColorFromRGB(0xFFD700);
        _evaluateView.numberOfLines = 0;
        [self.contentView addSubview: _evaluateView];
    }
    return _evaluateView;
}
//销售量
- (UILabel *)someView
{
    if(_someView ==nil)
    {
        _someView =[[UILabel alloc]init];
        _someView.font = [UIFont systemFontOfSize:12];
        _someView.textColor = UIColorFromRGB(0x666666);
        _someView.numberOfLines = 0;
        [self.contentView addSubview:_someView];
    }
    return _someView;
}
//距离
- (UILabel *)juliView
{
    if(_juliView ==nil)
    {
        _juliView =[[UILabel alloc]init];
        _juliView.font = [UIFont systemFontOfSize:11];
        _juliView.textColor = UIColorFromRGB(0x666666);
        [self.contentView addSubview: _juliView];
    }
    return _juliView;
}
//优惠
- (UILabel *)youhuiView
{
    if(_youhuiView ==nil)
    {
        _youhuiView =[[UILabel alloc]init];
        _youhuiView.font = [UIFont systemFontOfSize:13];
        
        _youhuiView.numberOfLines = 0;
        [self.contentView addSubview: _youhuiView];
    }
    return _youhuiView;
}
- (UIView *)bai
{
    if(_bai ==nil)
    {
        _bai = [[UIView alloc]init];
        _bai.backgroundColor = UIColorFromRGB(0xffffff);
        [self.contentView addSubview:_bai];
    }
    return _bai;
}
- (UIView *)xian
{
    if(_xian ==nil)
    {
        _xian = [[UIView alloc]init];
        _xian.backgroundColor = UIColorFromRGB(0xefeff1);
        [self.contentView addSubview:_xian];
    }
    return _xian;
}
- (void)setData:(MainDetailData *)data
{
    _data = data;
    
    UIView *contentView = self.contentView;
    CGFloat margin = 10;
    
    //设置各控件的frame以及data
    
    //头像
    _iconView.sd_layout
    .leftSpaceToView(contentView,margin)
    .topSpaceToView(contentView,margin)
    .heightIs(35)
    .widthIs(35);
    
    //昵称
    _nameView.sd_layout
    .leftSpaceToView(_iconView,margin)
    .topSpaceToView(contentView,1.5 * margin)
    .heightIs(16);
    [_nameView setSingleLineAutoResizeWithMaxWidth:200];
    
    _priceView.sd_layout
    .rightSpaceToView(contentView,8)
    .topSpaceToView(contentView,margin)
    .heightIs(16);
    [_priceView setSingleLineAutoResizeWithMaxWidth:80];
    
    _labprice.sd_layout
    .rightSpaceToView(contentView,8)
    .topSpaceToView(_priceView,margin)
    .heightIs(16);
    [_labprice setSingleLineAutoResizeWithMaxWidth:80];
    
    //xing
    _xingImage.sd_layout
    .leftSpaceToView(_iconView,margin)
    .topSpaceToView(_nameView,margin)
    .heightIs(10)
    .widthIs(50);
    //星数
    
    _bai.sd_layout
    .rightEqualToView(_xingImage)
    .topSpaceToView(_nameView,margin)
    .heightIs(10)
    .widthIs(50 - [data.fen floatValue]*10);
    //back
    _backImage.sd_layout
    .leftSpaceToView(_iconView,margin)
    .topEqualToView(_xingImage)
    .heightIs(10)
    .widthIs(50);
    
    _evaluateView.sd_layout
    .leftSpaceToView(_xingImage,5)
    .topSpaceToView(_nameView,8)
    .heightIs(16);
    [_evaluateView setSingleLineAutoResizeWithMaxWidth:50];

    _someView.sd_layout
    .leftSpaceToView(_evaluateView,5)
    .topSpaceToView(_nameView,9)
    .heightIs(16);
    [_someView setSingleLineAutoResizeWithMaxWidth:50];
    
    _juliView.sd_layout
    .leftEqualToView(_nameView)
    .topSpaceToView(_xingImage,margin)
    .heightIs(16);
    [_juliView setSingleLineAutoResizeWithMaxWidth:200];

   
    _xian.sd_layout
    .leftEqualToView(_nameView)
    .topSpaceToView(_juliView,margin)
    .rightSpaceToView(contentView,2)
    .heightIs(1);

    _youhuiView.sd_layout
    .leftEqualToView(_nameView)
    .topSpaceToView(_xian,margin)
    .heightIs(16);
    [_youhuiView setSingleLineAutoResizeWithMaxWidth:300];

    _iconView.image= [UIImage imageNamed:data.image];
    _iconView.layer.cornerRadius = _iconView.width * 0.5;
    _iconView.clipsToBounds = YES;
    [_nameView setText:data.name];
    [_priceView setText:[NSString stringWithFormat:@"¥%@ 起送",data.price]];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:_priceView.text];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0,str.length-2)];
    [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:14.0] range:NSMakeRange(0, str.length-2)];
    _priceView.attributedText = str;
    
    [_labprice setText:[NSString stringWithFormat:@"¥%@ 配送价",data.price]];
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:_labprice.text];
    [str1 addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0,str1.length-3)];
    [str1 addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:13.0] range:NSMakeRange(0, str1.length-3)];
    _labprice.attributedText = str1;

    [_evaluateView setText:data.fen];
    [_someView setText:[NSString stringWithFormat:@"月售%@单",data.numer]];
    [_juliView setText:[NSString stringWithFormat:@"距此%@千米",data.juli]];
    [_youhuiView setText:data.favourable];

    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self= [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.userInteractionEnabled = YES;
        self.selectionStyle=UITableViewCellSelectionStyleNone;
        [self iconView];//头像
        [self nameView];//昵称
        [self priceView];//起送价
        [self evaluateView];//评分
        [self someView];//销售量
        [self juliView];//距离
        [self youhuiView];//优惠信息
        [self xingImage];
        [self bai];
        [self backImage];
        [self labprice];
        [self xian];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
